package com.example.plantplacessummer2013;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.plantplaces.dto.Plant;
import com.plantplaces.dto.PlantSearch;
import com.plantplaces.service.IPlantService;
import com.plantplaces.service.PlantService;

public class PlantResultsActivity extends ListActivity {

	public static final String PLANT_ID = "PLANT_ID";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// I want to pull out the plant search object.
		PlantSearch ps = (PlantSearch) this.getIntent().getSerializableExtra(AdvancedPlantSearch.PLANT_SEARCH);

		try {
			// invoke the AsyncTask to get our results.
			PlantSearchTask pst = new PlantSearchTask();
			// pass the PlantSearchTask our search criteria, which is represented in the ps object, and tell it
			// to search for plants on the background thread.
			pst.execute(ps);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			// inform the user there was an error.
			Toast.makeText(this, getString(R.string.errUnableToGetResults), Toast.LENGTH_LONG).show();
		}

	}


	/**
	 * Find out which plant the user clicked, go to the details page.
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// Find the plant that the user selected.
		Plant selectedPlant = (Plant) getListView().getItemAtPosition(position);

		// create an intent to naviate to the web details page.
		Intent plantDetailsIntent = new Intent(this, WebViewActivity.class);

		// find the plant ID, and put it into the intent.
		int plantId = selectedPlant.getId();
		plantDetailsIntent.putExtra(PLANT_ID, plantId);

		// go to the plant details page for this plant.
		startActivity(plantDetailsIntent);
	}

	/**
	 * AsyncTask will allow us to take heavy-lifting operations off of the UI thread.
	 * 
	 * @author jonesbr
	 *
	 */
	class PlantSearchTask extends AsyncTask<PlantSearch, Integer, List<Plant>> {

		/**
		 * This method will run in the background thread.
		 */
		@Override
		protected List<Plant> doInBackground(PlantSearch... params) {
			IPlantService plantService = new PlantService(PlantResultsActivity.this);

			// invoke the search.
			List<Plant> results = new ArrayList<Plant>();

			try {
				// this is the network operation that will run in the new thread.
				results = plantService.fetchPlants(params[0]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// return the plants that we got from the network.
			return results;
		}


		/**
		 * This method will run when the background thread is complete.  
		 * This method will run in the UI thread, so we can update the UI.
		 */
		@Override
		protected void onPostExecute(List<Plant> result) {
			// marry togehether the data with the screen.
			ArrayAdapter<Plant> listAdapter = new ArrayAdapter<Plant>(PlantResultsActivity.this, android.R.layout.simple_list_item_1, result);

			// show the data.
			setListAdapter(listAdapter);		
		}


	}


}
